package com.campus.controller;

import com.campus.constant.CourseLearnUnitConstant;
import com.campus.entry.CourseLearnUnit;
import com.campus.pojo.dto.CourseLearnUnitDto;
import com.campus.pojo.dto.StudentLearningUnitDto;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.CourseLearnUnitService;
import com.campus.service.ManagerStudentService;
import com.campus.service.ManagerTeacherService;
import com.campus.util.GetUserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseLearnUnit")
public class CourseLearnUnitController {

    @Autowired
    private CourseLearnUnitService courseLearnUnitService;
    @Autowired
    private ManagerTeacherService managerTeacherService;
    @Autowired
    private ManagerStudentService managerStudentService;


    /**
     * 根据教师Id查询单元列表
     * @param titleName
     * @param status
     * @return {@link ResponseResult }<{@link List }<{@link CourseLearnUnitDto }>>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @GetMapping("/getByTeacherId")
    public ResponseResult<List<CourseLearnUnitDto>> getCourseLearnUnitsByTeacherId(@RequestParam(required = false) String titleName,@RequestParam(required = false) Integer status) {
        Integer teacherId=managerTeacherService.getTeacherIdByLoginId(GetUserIdUtil.getUserId());
        List<CourseLearnUnitDto> courseLearnUnits = courseLearnUnitService.getCourseLearnUnitsByTeacherId(teacherId,titleName,status);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,courseLearnUnits);
    }
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PutMapping("/close/{id}")
    public ResponseResult<Object> closeUnit(@PathVariable Integer id){
        courseLearnUnitService.updateState(id, CourseLearnUnitConstant.CLOSE);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PutMapping("/open/{id}")
    public ResponseResult<Object> openUnit(@PathVariable Integer id){
        courseLearnUnitService.updateState(id,CourseLearnUnitConstant.OPEN);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 根据id删除单元
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @DeleteMapping("/{id}")
    public ResponseResult<Object> deleteUnit(@PathVariable Integer id){
        courseLearnUnitService.deleteUnit(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 教师查询单元
     * @param id
     * @return {@link ResponseResult }<{@link CourseLearnUnit }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @GetMapping("/getUnitById/{id}")
    public ResponseResult<CourseLearnUnit> getCourseById(@PathVariable Integer id){
        CourseLearnUnit courseLearnUnit=courseLearnUnitService.getUnitById(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,courseLearnUnit);
    }

    /**
     * 教师更新单元信息
     * @param courseLearnUnit
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PutMapping("/update")
    public ResponseResult<Object> updateUnit(@RequestBody CourseLearnUnit courseLearnUnit){
        courseLearnUnitService.updateUnit(courseLearnUnit);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 教师新增单元信息
     * @param courseLearnUnit
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PostMapping("/save")
    public ResponseResult<Object> saveUnit(@RequestBody CourseLearnUnit courseLearnUnit){
        courseLearnUnitService.saveUnit(courseLearnUnit);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 学生查询学习单元
     * @return {@link ResponseResult }<{@link List }<{@link StudentLearningUnitDto }>>
     */
    @PreAuthorize("hasAnyRole({'student'})")
    @GetMapping("/getUnitByStudent")
    public ResponseResult<List<StudentLearningUnitDto>> getUnitByStudent(){
        Integer studentId=managerStudentService.getStudentIdByLoinId(GetUserIdUtil.getUserId());
        List<StudentLearningUnitDto> list=courseLearnUnitService.getUnitByStudent(studentId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,list);
    }
}
